package com.jh3502.helper.function.batch.mytask;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jh3502.helper.R;
import com.jh3502.helper.utils.Constant;
import com.jh3502.helper.utils.SpHelper;
import com.jh3502.helper.utils.Utils;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyTaskAdapter extends RecyclerView.Adapter<MyTaskAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {


    private Context mContext;
    private ArrayList<Map<String, Object>> listItems;
    //    private String batInfoTemp;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null;
    private ButtonInterface viewClickTest, viewClickConfig, viewClickDelete,viewClickView;

    public MyTaskAdapter(Context context, ArrayList<Map<String, Object>> datas) {
        this.listItems = datas;
        this.mContext = context;
    }

    public void setButtonSetOnclickTest(ButtonInterface buttonInterface) {
        this.viewClickTest = buttonInterface;
    }

    public void setButtonSetOnclickConfig(ButtonInterface buttonInterface) {
        this.viewClickConfig = buttonInterface;
    }

    public void setButtonSetOnclickDelete(ButtonInterface buttonInterface) {
        this.viewClickDelete = buttonInterface;
    }

    public void setButtonSetOnclickView(ButtonInterface buttonInterface) {
        this.viewClickView = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_home1, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        convertView.setOnLongClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        Map<String, Object> batInfoMap = Utils.getInstance().jso2map(SpHelper.getInstance().getBatAct());
        viewHolder.itemView.setTag(position + "");
        viewHolder.itemHome1Num.setText(position + "");

        if(position%2!=0){
            viewHolder.itemHome1.setBackgroundColor(ContextCompat.getColor(mContext,R.color.gray_bg));
        }else{
            viewHolder.itemHome1.setBackgroundColor(Color.WHITE);
        }


        String batno = (String) listItems.get(position).get("batno");
        viewHolder.itemHome1Id.setText(batno);

        viewHolder.itemHome1Id.setText(batno);
        viewHolder.itemHome1Area.setText((String) listItems.get(position).get("region"));
        viewHolder.itemHome1Count.setText((String) listItems.get(position).get("number"));
        String status = (String) listItems.get(position).get("status");
        if (status.equals(Constant.BATCH_FINAL)) {
            viewHolder.itemHome1State.setText("已完成");
            viewHolder.itemHome1State.setTextColor(Color.parseColor("#999999"));

            viewHolder.itemHome1StateImg.setVisibility(View.VISIBLE);
            viewHolder.itemHome1Button1.setVisibility(View.GONE);
            viewHolder.itemHome1Button2.setVisibility(View.VISIBLE);

        } else {
            viewHolder.itemHome1State.setText("未开始");
            viewHolder.itemHome1State.setTextColor(Color.parseColor("#f04e53"));
            viewHolder.itemHome1StateImg.setVisibility(View.GONE);
            viewHolder.itemHome1Button1.setVisibility(View.VISIBLE);
            viewHolder.itemHome1Button2.setVisibility(View.GONE);
            viewHolder.itemHome1.setBackgroundColor(Color.parseColor("#fef3f2"));
        }
        if (batInfoMap.size() > 0) {
            if (batInfoMap.get("batno").equals(batno)) {
                viewHolder.itemHome1.setBackgroundColor(Color.parseColor("#f1fff3"));
                viewHolder.itemHome1State.setText("进行中");
                viewHolder.itemHome1State.setTextColor(Color.parseColor("#40b542"));
            }
        }
        viewHolder.itemHome1Date.setText((String) listItems.get(position).get("createTime"));
        viewHolder.itemHome1Test.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickTest != null) {
                //接口实例化后的而对象，调用重写后的方法
                viewClickTest.onClick(position);
            }
        });
        viewHolder.itemHome1Config.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickConfig != null) {
                //接口实例化后的而对象，调用重写后的方法
                viewClickConfig.onClick(position);
            }
        });
        viewHolder.itemHome1Deldete.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickDelete != null) {
                //接口实例化后的而对象，调用重写后的方法
                viewClickDelete.onClick(position);
            }
        });
        viewHolder.itemHome1View.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickView != null) {
                //接口实例化后的而对象，调用重写后的方法
                viewClickView.onClick(position);
            }
        });
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(@NonNull View v) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick((String) v.getTag());
        }
        return true;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(String s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_home1_num)
        TextView itemHome1Num;
        @BindView(R.id.item_home1_id)
        TextView itemHome1Id;
        @BindView(R.id.item_home1_area)
        TextView itemHome1Area;
        @BindView(R.id.item_home1_count)
        TextView itemHome1Count;
        @BindView(R.id.item_home1_state)
        TextView itemHome1State;
        @BindView(R.id.item_home1_date)
        TextView itemHome1Date;
        @BindView(R.id.item_home1_config)
        Button itemHome1Config;
        @BindView(R.id.item_home1_test)
        Button itemHome1Test;
        @BindView(R.id.item_home1_deldete)
        Button itemHome1Deldete;
        @BindView(R.id.item_home1_view)
        Button itemHome1View;
        @BindView(R.id.item_home1_state_img)
        ImageView itemHome1StateImg;
        @BindView(R.id.item_home1_button1)
        LinearLayout itemHome1Button1;
        @BindView(R.id.item_home1_button2)
        LinearLayout itemHome1Button2;
        @BindView(R.id.item_home1)
        LinearLayout itemHome1;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}